/******************************************************************************
*
* This file is part of FreeTrack (http://code.google.com/p/qtfreetrack).
* BY Willy Gardiol (willy@gardiol.org)
*
* FreeTrack is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FreeTrack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar.  If not, see <http://www.gnu.org/licenses/>
*
******************************************************************************/
#ifndef TRACKGPS_H
#define TRACKGPS_H

#include "IncFreeTrack.h"
#include "freetrackwidget.h"
#include "trackpoint.h"

class FTPushButton;
class FTLabel;
class FTHelpWidget;

class LayerTrack;

/**
  TrackGps class is the interface between the device GPS and FreeTrack itself.

  Basically, it exposes two signals and one public method. The signals can be used
  to received the updated position or the acquisition/loss of a fix.

  The method can be called to receive the last know position.

  There is also a "debug" mode (which can be activated in the pro file). In this mode, the
  GPS device will not be active and position will be read from a text file instead.

  To use the debug mode, please check in the constructor. You need to populate a e:\debugPoints.txt
  file with the save auto-save format data.
*/

class TrackGps : public FreeTrackWidget
{
    Q_OBJECT

public:
    explicit TrackGps(QWidget *parent);
    ~TrackGps();

    QPointF currentPosition();
    // This is required to distribute the initial GPS position after all items have been initialized.
    void postInitialize();

signals:
    // Emitted when the GPS has a fix/has lost a fix
    void gpsStatusChange(bool);
    // Emitted every new GPS point received.
    void gpsUpdate( const TrackPoint& );


public slots:
    void gpsTimeout();
    void updateFromGps(QGeoPositionInfo gpsPos);

private:
    // Store the current position
    TrackPoint current_position;
    // Keep track of GPS not fixed
    bool gpx_has_fix;
    // check to update speed label when speed is calculated
    bool speed_label_calc;
    // Timer to mark GPS as timeout
    QTimer* gps_timer;

    // Items for the UI
    FTLabel* position;
    FTLabel* altitude;
    FTLabel* precision;
    FTLabel* speed;
    FTLabel* position_label;
    FTLabel* altitude_label;
    FTLabel* precision_label;
    FTLabel* speed_label;
    FTHelpWidget* gpsHelp;

    // The object to access the GPS device
    QPointer<QGeoPositionInfoSource> m_location;

    QLayout* createHorizontal();
    QLayout* createVertical();

#ifdef GPS_DEBUG
    QList<TrackPoint> gpsPoints;
    QTimer * gps_debug_timer;
public slots:
    void debugUpdateFromGps();
#endif

};
#endif // TRACKGPS_H
